package com.sbtr.common.extraconfig;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

/**
 * 针对Long类型的数据Json转换配置
 *
 * @author qiujun
 * @date 2019-10-25
 */
@Configuration
public class JsLongConfig implements WebMvcConfigurer {
	
	public static final Charset UTF8 = Charset.forName("UTF-8");
	
    private FastJsonHttpMessageConverter customConverters() {
        FastJsonHttpMessageConverter fastJsonMsgConvert = new JsLongFastJsonHttpMessageConverter();
        fastJsonMsgConvert.setSupportedMediaTypes(
                Arrays.asList(
                        new MediaType("application", "json", UTF8),
                        new MediaType("application", "*+json", UTF8)));
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteMapNullValue);
        fastJsonMsgConvert.setFastJsonConfig(fastJsonConfig);
        return fastJsonMsgConvert;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(0, customConverters());
    }

}
